/*
 * @Author: szx
 * @Date: 2021-12-25 13:56:03
 * @LastEditTime: 2021-12-25 14:01:24
 * @Description:
 * @FilePath: \leetcode\200-299\235\235.js
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
var lowestCommonAncestor = function (root, p, q) {
    if (!root) return null;
    if ((root.val <= p.val && root.val >= q.val) || (root.val >= p.val && root.val <= q.val)) return root;
    if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
    if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
};
